<?php
/* +----------------------------------------------------------------------
   | 管理员控制器
   +----------------------------------------------------------------------*/

namespace Admin\Controller;

use Think\Controller;

Class AdminController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 绑定坐席
     */
    public function edit_agent()
    {
        $agent=I('agent');
        $agent_s=$agent.'@hyyjr';
        $agents=M('agent','','KEFU')->getField('agent',true);
        if(!in_array($agent_s,$agents)){
            $this->ajax('1002', '错误的坐席号');
            die;
        }
        $data=M('agent','','KEFU')->where(['agent'=>$agent_s])->field('agent,pwd')->find();
        $arr=array('agent'=>$data['agent'],'agent_pwd'=>$data['pwd']);
        $arr1=array('agent'=>"",'agent_pwd'=>"");
        M('admin','','KEFU')->where(['agent'=>$data['agent']])->save($arr1);
        if(M('admin','','KEFU')->where(['id'=>$_SESSION['id']])->save($arr)){
            $this->ajax('1001', '绑定成功');
            die;
        }
        $this->ajax('1002', '绑定失败');
        die;
    }

    /**
     * 获取所有电话坐席
     */
    public function get_agent()
    {
        $admmodel = M('admin', '', 'KEFU');
        $where['status'] = 1;
        $where['agent_status'] = 1;
        $where['id'] = array('neq', $_SESSION['id']);
        $admin = $admmodel->where($where)->field('id,name,agent')->select();
        $str_name = '';
        $str_agent = '';
        foreach ($admin as $k => $v){
            $agent=substr($v['agent'],0,4);
                $str_name =$str_name.$v['name'].',' ;
                $str_agent =$str_agent.$agent.',' ;
        }
        echo $str_name.'.'.$str_agent;
    }
    /**
     * 管理员列表
     */
    public function adminlist()
    {
        $admmodel = M('admin', '', 'KEFU');
        $where['a.status'] = ['neq', 3];
        $tol = $admmodel->alias('a')->join('role b on a.rid=b.id')->where($where)->count('a.id');
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = $admmodel->alias('a')->join('role b on a.rid=b.id')->where($where)->field('a.id,a.name,a.phone,a.status,a.ctime,b.rolename as rname')
            ->LIMIT($page->listfirst, $page->listRows)->select();
        foreach ($data as $k => $v) {
            if (!empty($v['ctime'])) {
                $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            }
            switch ($v['status']) {
                case 1:
                    $data[$k]['status'] = '正常';
                    break;
                case 2:
                    $data[$k]['status'] = '锁定';
                    break;
                case 3:
                    $data[$k]['status'] = '删除';
                    break;
            }
        }


        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     * 修改管理员
     */
    public function modifyadm($data, $id = '')
    {
        $admmodel = M('admin', '', 'KEFU');
        if (!empty($id)) {
            if ($admmodel->where(['id' => $id])->save($data)) {
                $ret['state'] = 1;
                $ret['msg'] = '修改成功';
            } else {
                $ret['state'] = 1;
                $ret['msg'] = '修改失败';
            }
        } else {
            if ($admmodel->add($data)) {
                $ret['state'] = 1;
                $ret['msg'] = '添加成功';
            } else {
                $ret['state'] = 1;
                $ret['msg'] = '添加失败';
            }
        }
        $this->ajaxreturn($ret, 'json');
    }

    /**
     * 创建账号页
     */
    public function add()
    {
        $rolemodel = M('role', '', 'KEFU');
        $this->role = $rolemodel->where(['status' => 1])->field('id,name')->select();
        $this->display();
    }

    /**
     * 修改页
     */
    public function edit()
    {
        $rolemodel = M('role', '', 'KEFU');
        $this->role = $rolemodel->where(['status' => 1])->field('id,name')->select();
        $admmodel = M('admin', '', 'KEFU');
        $this->data = $admmodel->where(['id' => I('id')])->field('pwd,ctime', true)->find();
        $this->display();
    }

    /**
     * 创建管理员
     */
    public function addadm()
    {
        $data = I('post.');
        $phone = M('admin', '', 'KEFU')->where(['phone' => $data['phone']])->getfield('phone');
        if (!empty($phone)) {
            $this->ajaxreturn(['state' => 4], 'json');
        }
        $data['ctime'] = time();
        $data['pwd'] = set_pwd('123456', $phone);
        $this->modifyadm($data, '');
    }

    /**
     * 修改管理员
     */
    public function editadm()
    {
        $data = I('post.');
        $this->modifyadm($data, $data['id']);
    }

    /**
     * 重置密码
     */
    public function editpwd()
    {
        $data = I('post.');
        $data['pwd'] = set_pwd('123456', $data['phone']);;
        $this->modifyadm($data, $data['id']);
    }

    /**
     * 修改页
     */
    public function modifypwd()
    {
        $admmodel = M('admin', '', 'KEFU');
        $this->data = $admmodel->where(['id' => $_SESSION['id']])->field('pwd,ctime', true)->find();
        //总回复量
        $work = strtotime(date('Y-m-d 09:00:00', time()));
        $off_work = strtotime(date('Y-m-d 20:30:00', time()));
        $where = [];
        $where['vid'] = '0';
        $where['admin_name'] = $_SESSION['name'];
//        $total=M('user_message', '', 'KEFU')->where($where)->group('uf_id')->select();

        $where['ctime'] = array('BETWEEN', array($work, $off_work));
        $feedback_num = M('user_message', '', 'KEFU')->where($where)->group('uf_id')->select();

        $this->today = count($feedback_num);
//        $this->total=count($total);
        $this->display();
    }

    /**
     * 修改自己的密码
     */
    public function editpwdfunc()
    {

        $admmodel = M('admin', '', 'KEFU');
        $where['pwd'] = set_pwd(I('post.oldpwd'), I('post.phone'));
        $where['phone'] = I('post.phone');
        if (!$admmodel->where($where)->getfield('id')) {
            $this->ajaxreturn(['state' => 2, 'msg' => '旧密码错误'], 'json');
        }
        $data['pwd'] = set_pwd(I('post.newpwd'), I('post.phone'));
        $this->modifyadm($data, I('id'));
    }


}